<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="渠道：">
            <el-select
              v-model="listQuery.channel"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in channelList"
                :label="item.commonDesc"
                :key="item.commonCode"
                :value="item.commonCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="弹屏位置：">
            <el-select
              v-model="listQuery.popId"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in popIdType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="名称：">
            <el-input
              v-model="listQuery.title"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()"
        >新增</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column label="ID" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="名称" align="center">
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="位置" align="center" width="200">
          <template slot-scope="scope">
            <el-tag
              v-for="(item, index) in formatPopId(scope.row.popId)"
              :key="index"
              >{{ popIdOptions[item] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="图片" width="100" align="center">
          <template slot-scope="scope">
            <img style="height: 40px" :src="scope.row.content" />
          </template>
        </el-table-column>
        <el-table-column label="渠道" align="center">
          <template slot-scope="scope">{{
            channelOptions[`${scope.row.channel}`]
          }}</template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">{{
            statusList[scope.row.status]
          }}</template>
        </el-table-column>
        <el-table-column label="生效日期" align="center">
          <template slot-scope="scope">{{
            scope.row.stime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="失效日期" align="center">
          <template slot-scope="scope">{{
            scope.row.etime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="发布日期" align="center">
          <template slot-scope="scope">{{
            scope.row.createTime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status === '4'"
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  contentList,
  delByIdTrue,
  commonList
} from '@/api/content'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  classified: '10'
}
export default {
  name: 'List',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      loading: false,
      channelList: [],
      statusList: ['', '未生效', '未生效', '未生效', '生效中', '已失效'],
      popIdOptions: { '1': '首页', '2': '社区', '3': '学院', '4': '学习', '5': '服务' },
      channelOptions: {},
      popIdType: [
        { label: '全部', value: '' },
        { label: '首页', value: '1' },
        { label: '社区', value: '2' },
        { label: '学院', value: '3' },
        { label: '学习', value: '4' },
        { label: '服务', value: '5' }]
    }
  },
  created () {
    this.getChannelType()
    this.getList()
  },
  filters: {
    formatCreateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    formatPopId (data) {
      return data.split(',')
    },
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteById(row.id)
    },
    handleReview (index, row) {
      this.$router.push({
        path: '/cont-ms/reviewPop',
        query: { id: row.id }
      })
    },
    handleAdd () {
      this.$router.push({ path: '/cont-ms/addPop' })
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/cont-ms/updatePop',
        query: { id: row.id }
      })
    },
    getList () {
      this.listLoading = true
      const { courseType, deptId } = this.listQuery
      const params = {
        ...this.listQuery
      }
      if (courseType && courseType.length > 0) {
        params.courseType = courseType[courseType.length - 1]
      }
      if (deptId) {
        params.deptName = deptId
      }
      contentList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    deleteById (ids) {
      this.$confirm('是否要删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('id', ids)
        delByIdTrue(params).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    getChannelType () {
      const params = {
        commonType: 'channel'
      }
      commonList(params).then((response) => {
        this.channelList = response.data
        response.data.map((item) => {
          this.channelOptions[`${item.commonCode}`] = item.commonDesc
        })
      })
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
